package com.twang.stock.design.impl;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.design.IndexDataTask;
import com.twang.stock.dto.IndexedInfo;
import com.twang.stock.entity.ThsHotStock;
import com.twang.stock.entity.ThsReDianStock;
import com.twang.stock.mapper.ThsReDianStockMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.IndexService;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 24小时同花顺热度榜单
 * @author
 */
@Service
@Slf4j
public class ThsHotStockListTask extends IndexedInfo implements IndexDataTask {
    public static String cache_key = "ThsHotStockListTask";


    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private ThsReDianStockMapper thsReDianStockMapper;

    public static Integer dataSize=10;
    @Autowired
    private IndexService indexService;

    @Override
    public void execute(ModelAndView modelAndView, CountDownLatch latch) throws Exception {
        try {
            List<ThsReDianStock> list = thsReDianStockMapper.select24HourData();

            List<ThsHotStock> thsHotStockList= Lists.newArrayList();
            //查询
            Object obj = redisUtil.get(cache_key);
            if (null == obj ) {
                indexService.getThsHotStockList(list,thsHotStockList,dataSize);
                redisUtil.set(cache_key,JSONObject.toJSONString(thsHotStockList),60*60*24);
            } else {
                thsHotStockList = JSONObject.parseArray(obj.toString(), ThsHotStock.class);
            }

            modelAndView.addObject("thsHotStockList",thsHotStockList);
            log.info("24小时同花顺热度榜单 = {}", JSONObject.toJSONString(thsHotStockList));
            //
            setThsHotStockList(JSONObject.toJSONString(thsHotStockList));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            log.info("24小时同花顺热度榜单 执行完成，当前剩余条数:{}", latch.getCount());
            latch.countDown();
        }
    }
}

